import conversion.utils.csv.CsvFileUtil;

import java.io.IOException;
import java.util.Scanner;

public class CommandLineInterface {

    public static void main(String[] args) {

        // java -Xmx2g -jar code-conversion.jar -c input D:\CSV_working.csv output D:\result.json
        // java -Xmx2g -jar code-conversion.jar -c input D:\mini.csv output D:\result.json

        boolean verbose = false;
        boolean convert = false;

        String input = "";//输入路径
        String output = "";//输出路径

        String inputType = "";
        String outputType = "";

        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-h":
                case "--help":
                    printHelp();
                    System.exit(0);
                    break;
                case "-v":
                    verbose = true;
                    break;
                case "-c":
                    convert = true;
                    break;
                case "input":
                    if (i < args.length - 1 && !args[i + 1].startsWith("-")) {
                        inputType = getExtension(args[++i]);
                        if (inputType.equals("json") || inputType.equals("xml") || inputType.equals("yml") || inputType.equals("yaml") || inputType.equals("csv")) {
                            input = args[i];
                        } else {
                            System.err.println("Invalid input file type. Only JSON, XML, CSV and YAML files are allowed.");
                            System.exit(1);
                        }
                    } else {
                        System.err.println("Invalid input parameter.");
                        System.exit(1);
                    }
                    break;
                case "output":
                    if (i < args.length - 1 && !args[i + 1].startsWith("-")) {
                        outputType = getExtension(args[++i]);
                        if (outputType.equals("json") || outputType.equals("xml") || outputType.equals("yml") || outputType.equals("yaml") || outputType.equals("csv")) {
                            output = args[i];
                        } else {
                            System.err.println("Invalid output file type. Only JSON, XML, CSV and YAML files are allowed.");
                            System.exit(1);
                        }
                    } else {
                        System.err.println("Invalid output parameter.");
                        System.exit(1);
                    }
                    break;
                default:
                    System.err.println("Unknown option: " + args[i]);
                    System.err.println("Type 'java command.CommandLineInterface -h' or 'java command.CommandLineInterface --help' for usage.");
                    System.exit(1);
                    break;
            }
        }

        if (verbose) {
            System.out.println("This is 1.0 version.");
        }
        if (convert) {
            if (input.isEmpty() || output.isEmpty()) {
                System.err.println("Input or output parameter missing.");
                System.exit(1);
            }
            if (input.equals(output)) {
                System.err.println("Input equals output.");
                System.exit(1);
            }
            System.out.println("Start Convert...");

            long startTime = System.currentTimeMillis();

            convert(input, output, inputType, outputType);

            long endTime = System.currentTimeMillis();
            long elapsedTime = endTime - startTime;

            System.out.println("程序执行时间为：" + elapsedTime / 1000 + "秒");
        }
    }

    private static void convert(String inputPath, String outputPath, String inputType, String outputType) {
        String separator = "";

        if (inputType.equals("csv") || outputType.equals("csv")) {
            System.out.println("Please input CSV separator: ");
            Scanner sc = new Scanner(System.in);
            separator = sc.nextLine();
        }

        if (!inputType.equals(outputType)) {
            if (inputType.equals("csv")) {
                try {
                    CsvFileUtil.readAndProcessFile(inputPath, outputPath,outputType);
                } catch (Exception e) {
                    System.out.println("IO 错误: " + e.getMessage());
                }
            }
        }

    }

    private static String getExtension(String fileName) {
        int lastDotIndex = fileName.lastIndexOf('.');
        if (lastDotIndex == -1) {
            return "";
        } else {
            return fileName.substring(lastDotIndex + 1).toLowerCase();
        }
    }

    private static void printHelp() {
        System.out.println("Usage: java -jar code-conversion.jar [options]");
        System.out.println("Options:");
        System.out.println("  -h, --help                  Print this help message.");
        System.out.println("  -v                          Enable verbose mode.");
        System.out.println("  -c                          Enable conversion mode.");
        System.out.println("  input <file>                Specify the input file path.");
        System.out.println("  output <file>               Specify the output file path.");
        System.out.println("Supported data types: json, xml, csv, yaml");
    }
}
